<template>
  <PageWrapper dense contentFullHeight fixedHeight contentClass="flex">
    <BasicTable
      @register="registerTableItem"
      @row-click="handleRowClick"
      class="w-2/5 xl:w-2/5 mr-2"
    >
      <template #toolbar>
        <a-button type="primary" v-auth="'dic:addItems'" @click="handleItemCreate">{{
          t('新增项')
        }}</a-button>
      </template>
      <template #action="{ record }">
        <TableAction
          stopButtonPropagation
          :actions="[
            {
              icon: 'clarity:note-edit-line',
              auth: 'dic:edit',
              onClick: handleItemEdit.bind(null, record),
            },
            {
              icon: 'ant-design:delete-outlined',
              auth: 'dic:delete',
              color: 'error',
              popConfirm: {
                title: t('是否确认删除'),
                confirm: handleItemDelete.bind(null, record),
              },
            },
          ]"
        />
      </template>
    </BasicTable>
    <BasicTable @register="registerTableDetail" class="w-3/5 xl:w-3/5">
      <template #toolbar>
        <a-button type="primary" v-auth="'dic:addDictionary'" @click="handleDetailCreate">{{
          t('新增字典')
        }}</a-button>
      </template>
      <template #action="{ record }">
        <TableAction
          stopButtonPropagation
          :actions="[
            {
              icon: 'clarity:note-edit-line',
              auth: 'dic:edit',
              onClick: handleDetailEdit.bind(null, record),
            },
            {
              icon: 'ant-design:delete-outlined',
              auth: 'dic:delete',
              color: 'error',
              popConfirm: {
                title: t('是否确认删除'),
                confirm: handleDetailDelete.bind(null, record),
              },
            },
          ]"
        />
      </template>
    </BasicTable>
    <DicItemDrawer @register="registerItemDrawer" @success="handleItemSuccess" />

    <DicDetailDrawer @register="registerDetailDrawer" @success="handleDetailSuccess" />
  </PageWrapper>
</template>
<script lang="ts">
  import { defineComponent, ref } from 'vue';

  import { BasicTable, useTable, TableAction, FormSchema, BasicColumn } from '/@/components/Table';
  import {
    getDicItemPageList,
    getDicDetailPageList,
    deleteDicItem,
    deleteDicDetail,
  } from '/@/api/system/dic';
  import { PageWrapper } from '/@/components/Page';

  import DicItemDrawer from './components/DicItemDrawer.vue';
  import DicDetailDrawer from './components/DicDetailDrawer.vue';

  import { useMessage } from '/@/hooks/web/useMessage';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useDrawer } from '/@/components/Drawer';
  const { t } = useI18n();
  export const searchFormSchemaItem: FormSchema[] = [
    {
      field: 'name',
      label: t('项目名'),
      component: 'Input',
      colProps: { span: 24, xs: 24, sm: 24, md: 24, lg: 24 },
    },
    {
      field: 'code',
      label: t('编码'),
      component: 'Input',
      colProps: { span: 24, xs: 24, sm: 24, md: 24, lg: 24 },
    },
  ];

  export const itemColumns: BasicColumn[] = [
    {
      title: t('项目名'),
      dataIndex: 'name',
      width: 120,
      align: 'left',
    },
    {
      title: t('编号'),
      dataIndex: 'code',
      width: 120,
      align: 'left',
    },
    {
      title: t('备注'),
      dataIndex: 'remark',
      align: 'left',
    },
  ];

  export const searchFormSchemaDetail: FormSchema[] = [
    {
      field: 'name',
      label: t('字典名'),
      component: 'Input',
      colProps: { lg: 8, md: 24 },
    },
    {
      field: 'code',
      label: t('编码'),
      component: 'Input',
      colProps: { lg: 8, md: 24 },
    },
  ];

  export const detailColumns: BasicColumn[] = [
    {
      title: t('字典名'),
      dataIndex: 'name',
      width: 120,
      align: 'left',
    },
    {
      title: t('编号'),
      dataIndex: 'code',
      width: 120,
      align: 'left',
    },
    {
      title: t('值'),
      dataIndex: 'value',
      width: 120,
      align: 'left',
    },
    {
      title: t('备注'),
      align: 'left',
      dataIndex: 'remark',
    },
  ];

  export default defineComponent({
    name: 'DicManagement',
    components: { BasicTable, PageWrapper, TableAction, DicItemDrawer, DicDetailDrawer },
    setup() {
      const { notification } = useMessage();

      const selectItemId = ref('');

      const [registerItemDrawer, { openDrawer: openItemDrawer }] = useDrawer();
      const [registerDetailDrawer, { openDrawer: openDetailDrawer }] = useDrawer();

      const [registerTableItem, { reload: itemReload }] = useTable({
        title: t('数据字典项'),
        api: getDicItemPageList,
        rowKey: 'id',
        columns: itemColumns,
        formConfig: {
          rowProps: {
            gutter: 16,
          },
          schemas: searchFormSchemaItem,
        },
        rowSelection: {
          type: 'radio',
          onChange: onSelectChange,
        },
        useSearchForm: true,
        showTableSetting: true,
        striped: false,
        actionColumn: {
          width: 80,
          title: t('操作'),
          dataIndex: 'action',
          slots: { customRender: 'action' },
        },
      });
      const [registerTableDetail, { reload: detailReload }] = useTable({
        title: t('数据字典详情'),
        api: getDicDetailPageList,
        rowKey: 'id',
        columns: detailColumns,
        formConfig: {
          rowProps: {
            gutter: 16,
          },
          schemas: searchFormSchemaDetail,
        },
        beforeFetch: (params) => {
          //发送请求默认新增  左边树结构所选机构id
          return { ...params, itemId: selectItemId.value };
        },
        useSearchForm: true,
        showTableSetting: true,
        striped: false,
        actionColumn: {
          width: 80,
          title: t('操作'),
          dataIndex: 'action',
          slots: { customRender: 'action' },
        },
      });

      function handleRowClick(record: any) {
        selectItemId.value = record.id;
        detailReload();
      }
      function onSelectChange(rowKey: string[]) {
        selectItemId.value = rowKey[0];
        detailReload();
      }
      function handleItemCreate() {
        openItemDrawer(true, {
          isUpdate: false,
        });
      }

      function handleDetailCreate() {
        if (!selectItemId.value) {
          notification.warning({
            message: t('提示'),
            description: t('没有选择任意一行字典项！'),
          });
          return;
        }

        openDetailDrawer(true, {
          itemId: selectItemId.value,
          isUpdate: false,
        });
      }

      function handleItemEdit(record: Recordable) {
        console.log(record);
        openItemDrawer(true, {
          record,
          isUpdate: true,
        });
      }
      function handleDetailEdit(record: Recordable) {
        console.log(record);
        openDetailDrawer(true, {
          record,
          isUpdate: true,
        });
      }

      function handleItemDelete(record: Recordable) {
        deleteDicItem([record.id]).then((_) => {
          itemReload();
          notification.success({
            message: t('提示'),
            description: t('删除成功'),
          }); //提示消息
        });
      }

      function handleDetailDelete(record: Recordable) {
        deleteDicDetail([record.id]).then((_) => {
          detailReload();
          notification.success({
            message: t('提示'),
            description: t('删除成功'),
          }); //提示消息
        });
      }

      function handleItemSuccess() {
        itemReload();
      }

      function handleDetailSuccess() {
        detailReload();
      }

      return {
        registerTableItem,
        registerTableDetail,
        registerItemDrawer,
        registerDetailDrawer,
        handleItemCreate,
        handleItemEdit,
        handleItemDelete,
        handleDetailCreate,
        handleDetailEdit,
        handleDetailDelete,
        handleItemSuccess,
        handleDetailSuccess,
        handleRowClick,
        t,
      };
    },
  });
</script>
